/***************************************************
 @Name :        小铭同学
 @Time :        2025-09-03
 @Email :       LiaoMingWJ233@163.com
 @Comment :     水表 电表 控件类
 @FileName :    waterelectricmeter.cpp
 @Version :     1.0
****************************************************/


#include "waterelectricmeter.h"
#include <QPainter>
#include <QConicalGradient>
#include <QtMath>
#include <QDebug>

WaterElectricMeter::WaterElectricMeter(QWidget *parent)
    : QWidget(parent)
    , m_value(0)
    , m_minValue(0)
    , m_maxValue(100)
    , m_meterType(WaterMeter)
    , m_unit("m³")
    , m_dialColor(QColor(240, 240, 240))
    , m_needleColor(QColor(255, 107, 107))
    , m_textColor(QColor(50, 50, 50))
{
    // 设置默认大小
    setMinimumSize(200, 200);
}

double WaterElectricMeter::value() const
{
    return m_value;
}

void WaterElectricMeter::setValue(double value)
{
    // 限制值在最小值和最大值之间
    value = qMax(m_minValue, qMin(value, m_maxValue));
    
    if (qFuzzyCompare(m_value, value))
        return;
    
    m_value = value;
    update(); // 触发重绘
    emit valueChanged(m_value);
}

double WaterElectricMeter::minValue() const
{
    return m_minValue;
}

void WaterElectricMeter::setMinValue(double minValue)
{
    m_minValue = minValue;
    // 确保当前值在范围内
    setValue(m_value);
    update();
}

double WaterElectricMeter::maxValue() const
{
    return m_maxValue;
}

void WaterElectricMeter::setMaxValue(double maxValue)
{
    m_maxValue = maxValue;
    // 确保当前值在范围内
    setValue(m_value);
    update();
}

WaterElectricMeter::MeterType WaterElectricMeter::meterType() const
{
    return m_meterType;
}

void WaterElectricMeter::setMeterType(MeterType type)
{
    if (m_meterType != type) {
        m_meterType = type;
        // 根据仪表类型设置默认单位
        if (type == WaterMeter) {
            m_unit = "m³";
        } else {
            m_unit = "kWh";
        }
        update();
    }
}

QString WaterElectricMeter::unit() const
{
    return m_unit;
}

void WaterElectricMeter::setUnit(const QString &unit)
{
    if (m_unit != unit) {
        m_unit = unit;
        update();
    }
}

QColor WaterElectricMeter::dialColor() const
{
    return m_dialColor;
}

void WaterElectricMeter::setDialColor(const QColor &color)
{
    if (m_dialColor != color) {
        m_dialColor = color;
        update();
    }
}

QColor WaterElectricMeter::needleColor() const
{
    return m_needleColor;
}

void WaterElectricMeter::setNeedleColor(const QColor &color)
{
    if (m_needleColor != color) {
        m_needleColor = color;
        update();
    }
}

QColor WaterElectricMeter::textColor() const
{
    return m_textColor;
}

void WaterElectricMeter::setTextColor(const QColor &color)
{
    if (m_textColor != color) {
        m_textColor = color;
        update();
    }
}

QSize WaterElectricMeter::sizeHint() const
{
    return QSize(300, 300);
}

QSize WaterElectricMeter::minimumSizeHint() const
{
    return QSize(150, 150);
}

void WaterElectricMeter::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); // 开启抗锯齿
    
    // 计算表盘大小和位置
    int side = qMin(width(), height());
    m_dialRect = QRectF(10, 10, side - 20, side - 20);
    
    // 绘制各个部件（移除了drawTitle）
    drawDial(&painter);
    drawScale(&painter);
    drawNeedle(&painter);
    drawValue(&painter);
}

void WaterElectricMeter::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    // 重新计算表盘矩形区域
    int side = qMin(width(), height());
    m_dialRect = QRectF(10, 10, side - 20, side - 20);
}

void WaterElectricMeter::drawDial(QPainter *painter)
{
    // 绘制表盘背景
    painter->save();
    
    QRadialGradient gradient(m_dialRect.center(), m_dialRect.width() / 2);
    gradient.setColorAt(0, m_dialColor.lighter(120));
    gradient.setColorAt(1, m_dialColor.darker(120));
    
    painter->setPen(Qt::NoPen);
    painter->setBrush(gradient);
    painter->drawEllipse(m_dialRect);
    
    // 绘制表盘边框
    painter->setPen(QPen(m_textColor.darker(150), 2));
    painter->setBrush(Qt::NoBrush);
    painter->drawEllipse(m_dialRect);
    
    painter->restore();
}

void WaterElectricMeter::drawScale(QPainter *painter)
{
    painter->save();
    painter->setPen(m_textColor);
    
    // 绘制主要刻度
    painter->translate(m_dialRect.center());
    
    for (int i = 0; i <= 10; ++i) {
        // 绘制刻度线
        painter->drawLine(0, -m_dialRect.width() / 2 + 10, 
                          0, -m_dialRect.width() / 2 + 20);
        
        // 绘制刻度值
        if (i % 2 == 0) {
            double value = m_minValue + (m_maxValue - m_minValue) * i / 10.0;
            QString text = QString::number(value, 'f', 0);
            
            painter->save();
            painter->translate(0, -m_dialRect.width() / 2 + 30);
            painter->rotate(180);
            painter->setFont(QFont("Arial", 8));
            QRect textRect(-20, -10, 40, 20);
            painter->drawText(textRect, Qt::AlignCenter, text);
            painter->restore();
        }
        
        painter->rotate(36); // 360度分成10份
    }
    
    painter->restore();
}

void WaterElectricMeter::drawNeedle(QPainter *painter)
{
    painter->save();
    painter->translate(m_dialRect.center());
    
    // 计算指针角度 (240度范围，从-120度到120度)
    double angle = -120 + (m_value - m_minValue) / (m_maxValue - m_minValue) * 240;
    painter->rotate(angle);
    
    // 绘制指针
    QPolygon needle;
    needle << QPoint(0, -m_dialRect.width() / 2 + 20)
           << QPoint(-5, 0)
           << QPoint(0, 10)
           << QPoint(5, 0);
    
    painter->setPen(Qt::NoPen);
    painter->setBrush(m_needleColor);
    painter->drawPolygon(needle);
    
    // 绘制指针中心圆
    painter->setPen(QPen(m_needleColor.darker(150), 2));
    painter->setBrush(m_needleColor.lighter(150));
    painter->drawEllipse(QRectF(-5, -5, 10, 10));
    
    painter->restore();
}

void WaterElectricMeter::drawValue(QPainter *painter)
{
    painter->save();
    
    // 在表盘中心下方显示当前值
    QRectF valueRect(m_dialRect.center().x() - 50, 
                    m_dialRect.center().y() + 20, 
                    100, 30);
    
    painter->setPen(m_textColor);
    painter->setFont(QFont("Arial", 12, QFont::Bold));
    
    QString valueText = QString("%1 %2").arg(m_value, 0, 'f', 1).arg(m_unit);
    painter->drawText(valueRect, Qt::AlignCenter, valueText);
    
    painter->restore();
}

void WaterElectricMeter::drawTitle(QPainter *painter)
{
    /*painter->save();
    
    // 在表盘顶部显示标题
    QRectF titleRect(m_dialRect.center().x() - 50, 
                    m_dialRect.top() + 10, 
                    100, 20);
    
    painter->setPen(m_textColor);
    painter->setFont(QFont("Arial", 10, QFont::Bold));
    
    QString title = (m_meterType == WaterMeter) ? "水表" : "电表";
    painter->drawText(titleRect, Qt::AlignCenter, title);
    
    painter->restore();*/
}
